/*
 * date          Author         Note
 * 2015.04.15    wzwz           起底板测试
 * 2015.07.16    wzwz           字符串bug
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include "mh/common.h"
#include "resource.h"
#include "func.h"
#include "lang.h"
#include "board_test.h"
#include "com.h"
#include "mhlabel.h"
#include "mhbtn.h"
#include "belt.h"
#include "keypane.h"
#include "info.h"
#include "board_param.h"
#include "clip.h"

/*
 *
 */
short GetBoardPulse(HWND hWnd)
{
    short ret = 0;
    HWND hTmp = GetDlgItem(hWnd, IDC_LABEL1 + 13);
    const char *sc = GetWindowCaption(hTmp);
    ret = atoi(sc);
    return ret;
}
/*
 *
 */
static void CreateBoardTestInterface(HWND hWnd)
{
    HWND hTmp = -1;
    //sys btn
    CreateWindow (MH_STATIC_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        ID_ESC,
        CX_BOARD_TEST_BMP - CX_ASK_BMP * 2,
        10,
        CX_ASK_BMP,
        CY_ASK_BMP,
        hWnd, (int)&g_bmp[BMP_FALSE]);
    CreateWindow (MH_STATIC_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        ID_ESC,
        CX_BOARD_TEST_BMP - CX_ASK_BMP * 3 - 8,
        10,
        CX_ASK_BMP,
        CY_ASK_BMP,
        hWnd, (int)&g_bmp[BMP_TRUE]);
    CreateWindow (MH_STATIC_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        ID_ESC,
        CX_BOARD_TEST_BMP - CX_ASK_BMP * 4 - 8 * 2,
        10,
        CX_ASK_BMP,
        CY_ASK_BMP,
        hWnd, (int)&g_bmp[BMP_ASK]);
    const char *sc[26] = {
            GetString(489), GetString(490), GetString(491),
            GetString(492), GetString(493), GetString(494),
            GetString(495), GetString(496), GetString(497),
            GetString(498), GetString(499), GetString(500),
            GetString(501), GetString(502), GetString(503),
            GetString(504), GetString(505), GetString(506),
            GetString(507), GetString(508), GetString(509),
            GetString(510), GetString(511), GetString(512),
            GetString(513), GetString(749),
    };
    int i = 0;
    for(i = 0; i < 26; ++i)
    {
        CreateWindow(MH_BTN_NAME,
            sc[i],
            WS_CHILD | WS_VISIBLE | BS_LEFT | BS_FLAT,
            ID_BUTTON1 + i,
            15 + (i / 9) * 210,
            60 + (i % 9) * 30,
            160,
            20,
            hWnd, MH_BTN_BKGND_BLACK);
    }
    const char *str[13] = {
        GetString(514), GetString(515), GetString(516), GetString(517),
        GetString(518), GetString(519), GetString(520), GetString(521),
        GetString(522), GetString(523), GetString(524), GetString(525),
        GetString(526),
    };
    for(i = 0; i < 13; ++i)
    {
        hTmp = CreateWindow(MH_LABEL_NAME,
            str[i],
            WS_CHILD | WS_VISIBLE,
            IDC_LABEL1 + i,
            30 + (i % 7) * 80,
            350 + (i / 7) * 30,
            60,
            20,
            hWnd, MH_LABEL_BKGND_BLUE | MH_LABEL_TEXT_WHITE);
        EnableWindow(hTmp, FALSE);
    }
    hTmp = CreateWindow(MH_LABEL_NAME,
        "00000",
        WS_CHILD | WS_VISIBLE,
        IDC_LABEL1 + 13,
        510,
        380,
        60,
        20,
        hWnd, MH_LABEL_BKGND_BLACK | MH_LABEL_TEXT_CYAN);
    EnableWindow(hTmp, FALSE);
}
/*
 *
 */
static void OnBtnCmd(HWND hWnd, int nID)
{
    WND_EXTRA_DATA data;
    data.value = GetWindowAdditionalData(hWnd);
    switch(nID)
    {
    case ID_ESC:
        {
            PostMessage(hWnd, MSG_CLOSE, 0, 0);
        }
        break;
    case ID_BUTTON1:
        {
            /*ClipOff(g_knit_data, 0x0F);
            data.board_test.cs_wait = 0;
            data.board_test.cs_act = TRUE;*/
        }
        break;
    case (ID_BUTTON1 + 1)...(ID_BUTTON1 + 4):
        {
            ClipOff(g_knit_data, 1 << (nID - ID_BUTTON1 - 1));
            data.board_test.cs_wait = 0;
            data.board_test.cs_act = TRUE;

            if(GetBoardParamCSSyn(g_knit_data))
            {
                if(nID < ID_BUTTON1 + 3)
                {
                    data.board_test.cs_syn = 1;
                }
                else
                {
                    data.board_test.cs_syn = 2;
                }
            }
        }
        break;
    case ID_BUTTON1 + 5:
        {
            /*ClipOn(g_knit_data, 0x0F);
            data.board_test.cs_wait = 0;
            data.board_test.cs_act = TRUE;*/
        }
        break;
    case (ID_BUTTON1 + 6)...(ID_BUTTON1 + 9):
        {
            ClipOn(g_knit_data, 1 << (nID - ID_BUTTON1 - 6));
            data.board_test.cs_wait = 0;
            data.board_test.cs_act = TRUE;
        }
        break;
    case ID_BUTTON1 + 10:
        if(data.board_test.roll_open)
        {
            data.board_test.board_reset = FALSE;
            BoardAct(g_knit_data, BOARD_4GRAB);
            data.board_test.board_wait = 0;
            data.board_test.board_act = TRUE;
        }
        else
        {
            AfxInfoBox(hWnd, NULL, GetString(751));
        }
        break;
    case ID_BUTTON1 + 11:
        if(data.board_test.roll_open)
        {
            data.board_test.board_reset = FALSE;
            BoardAct(g_knit_data, BOARD_4DROP);
            data.board_test.board_wait = 0;
            data.board_test.board_act = TRUE;
        }
        else
        {
            AfxInfoBox(hWnd, NULL, GetString(751));
        }
        break;
    case ID_BUTTON1 + 12:
        if(data.board_test.roll_open)
        {
            data.board_test.board_reset = FALSE;
            BoardAct(g_knit_data, BOARD_OFF);
            data.board_test.board_wait = 0;
            data.board_test.board_act = TRUE;
        }
        else
        {
            AfxInfoBox(hWnd, NULL, GetString(751));
        }
        break;
    case ID_BUTTON1 + 13:
        if(data.board_test.roll_open)
        {
            data.board_test.board_reset = FALSE;
            BoardAct(g_knit_data, BOARD_SAFE);
            data.board_test.board_wait = 0;
            data.board_test.board_act = TRUE;
        }
        else
        {
            AfxInfoBox(hWnd, NULL, GetString(751));
        }
        break;
    case ID_BUTTON1 + 14:
        if(data.board_test.roll_open)
        {
            HookOpen(g_knit_data);
            data.board_test.board_reset = TRUE;
        }
        else
        {
            AfxInfoBox(hWnd, NULL, GetString(751));
        }
        break;
    case ID_BUTTON1 + 15:
        {
            HookClose(g_knit_data);
        }
        break;
    case ID_BUTTON1 + 16:
        {
            HookOpen(g_knit_data);
        }
        break;
    case ID_BUTTON1 + 17:
        if(data.board_test.roll_open)
        {
            AfxInfoBox(hWnd, NULL, GetString(750));
        }
        else
        {
            data.board_test.roll_act = TRUE;
            RunRab(g_knit_data, -90, 20);
        }
        break;
    case ID_BUTTON1 + 18:
        if(data.board_test.roll_open)
        {
            data.board_test.board_reset = FALSE;
            BoardAct(g_knit_data, BOARD_MANUAL_UP);
            data.board_test.board_wait = 0;
            data.board_test.board_act = TRUE;
        }
        else
        {
            AfxInfoBox(hWnd, NULL, GetString(751));
        }
        break;
    case ID_BUTTON1 + 19:
        if(data.board_test.roll_open)
        {
            data.board_test.board_reset = FALSE;
            BoardAct(g_knit_data, BOARD_MANUAL_DOWN);
            data.board_test.board_wait = 0;
            data.board_test.board_act = TRUE;
        }
        else
        {
            AfxInfoBox(hWnd, NULL, GetString(751));
        }
        break;
    case ID_BUTTON1 + 20:
    case ID_BUTTON1 + 21:
        {
            Scissor(g_knit_data, 1 << (nID - ID_BUTTON1 - 20));
            data.board_test.cs_wait = 0;
            data.board_test.cs_act = TRUE;
        }
        break;
    case ID_BUTTON1 + 22:
        {
            CreateBoardParamWindow(hWnd);
        }
        break;
    case ID_BUTTON1 + 23:
        {
            //CreateClipWindow(hWnd);
        }
        break;
    case ID_BUTTON1 + 24:
        {
            ClipOff(g_knit_data, 1);
            data.board_test.cs_wait = 0;
            data.board_test.cs_auto = 1;
            data.board_test.cs_syn = 0;
            data.board_test.cs_act = TRUE;
        }
        break;
    case ID_BUTTON1 + 25:
        {
            data.board_test.board_reset = FALSE;
            BoardAct(g_knit_data, BOARD_RELAX);
        }
        break;
    }
    SetWindowAdditionalData(hWnd, data.value);
}
/*
 *
 */
static void OnKeyDown(HWND hWnd, uint key)
{
    switch(key)
    {
    case SCANCODE_ESCAPE:
        {
            OnBtnCmd(hWnd, ID_ESC);
        }
        break;
    case SCANCODE_0:
        {
            OnBtnCmd(hWnd, ID_BUTTON1);
        }
        break;
    case SCANCODE_1:
        {
            OnBtnCmd(hWnd, ID_BUTTON1 + 1);
        }
        break;
    case SCANCODE_2:
        {
            OnBtnCmd(hWnd, ID_BUTTON1 + 2);
        }
        break;
    case SCANCODE_3:
        {
            OnBtnCmd(hWnd, ID_BUTTON1 + 3);
        }
        break;
    case SCANCODE_4:
        {
            OnBtnCmd(hWnd, ID_BUTTON1 + 4);
        }
        break;
    case SCANCODE_5:
        {
            OnBtnCmd(hWnd, ID_BUTTON1 + 5);
        }
        break;
    case SCANCODE_6:
        {
            OnBtnCmd(hWnd, ID_BUTTON1 + 6);
        }
        break;
    case SCANCODE_7:
        {
            OnBtnCmd(hWnd, ID_BUTTON1 + 7);
        }
        break;
    case SCANCODE_8:
        {
            OnBtnCmd(hWnd, ID_BUTTON1 + 8);
        }
        break;
    case SCANCODE_9:
        {
            OnBtnCmd(hWnd, ID_BUTTON1 + 9);
        }
        break;
    case SCANCODE_A:
        {
            OnBtnCmd(hWnd, ID_BUTTON1 + 10);
        }
        break;
    case SCANCODE_B:
        {
            OnBtnCmd(hWnd, ID_BUTTON1 + 11);
        }
        break;
    case SCANCODE_C:
        {
            OnBtnCmd(hWnd, ID_BUTTON1 + 12);
        }
        break;
    case SCANCODE_D:
        {
            OnBtnCmd(hWnd, ID_BUTTON1 + 13);
        }
        break;
    case SCANCODE_E:
        {
            OnBtnCmd(hWnd, ID_BUTTON1 + 14);
        }
        break;
    case SCANCODE_F:
        {
            OnBtnCmd(hWnd, ID_BUTTON1 + 15);
        }
        break;
    case SCANCODE_F1:
        {
            OnBtnCmd(hWnd, ID_BUTTON1 + 16);
        }
        break;
    case SCANCODE_F2:
        {
            OnBtnCmd(hWnd, ID_BUTTON1 + 17);
        }
        break;
    case SCANCODE_PAGEUP:
        {
            OnBtnCmd(hWnd, ID_BUTTON1 + 18);
        }
        break;
    case SCANCODE_PAGEDOWN:
        {
            OnBtnCmd(hWnd, ID_BUTTON1 + 19);
        }
        break;
    case SCANCODE_F3:
        {
            OnBtnCmd(hWnd, ID_BUTTON1 + 20);
        }
        break;
    case SCANCODE_F4:
        {
            OnBtnCmd(hWnd, ID_BUTTON1 + 21);
        }
        break;
    case SCANCODE_F5:
        {
            OnBtnCmd(hWnd, ID_BUTTON1 + 22);
        }
        break;
    case SCANCODE_F6:
        {
            OnBtnCmd(hWnd, ID_BUTTON1 + 23);
        }
        break;
    case SCANCODE_F7:
        {
            OnBtnCmd(hWnd, ID_BUTTON1 + 25);
        }
        break;
    case SCANCODE_PERIOD:
        {
            OnBtnCmd(hWnd, ID_BUTTON1 + 24);
        }
        break;
    }
}
/*
 *
 */
static void OnIdle(HWND hWnd)
{
    WND_EXTRA_DATA data;
    data.value = GetWindowAdditionalData(hWnd);
    HWND hTmp = -1;
    BOARD_FEEDBACK feedback;
    static BOARD_FEEDBACK last_feedback;
    int ret = GetBoardFeedback(g_knit_data, &feedback);
    if(ret)
    {
        printf("board feedback error\n");
    }
    else if(feedback.can_error)
    {
        printf("board can error\n");
    }
    else
    {
        int down = MH_LABEL_BKGND_RED | MH_LABEL_TEXT_YELLOW;
        int up = MH_LABEL_BKGND_BLUE | MH_LABEL_TEXT_WHITE;
        int nStyle;

        if(feedback.hook_open != last_feedback.hook_open)
        {
            hTmp = GetDlgItem(hWnd, IDC_LABEL1);
            nStyle = feedback.hook_open ? down : up;
            SetLabelColorStyle(hTmp, nStyle);
            InvalidateRect(hTmp, NULL, TRUE);
        }

        if(feedback.hook_close != last_feedback.hook_close)
        {
            hTmp = GetDlgItem(hWnd, IDC_LABEL1 + 1);
            nStyle = feedback.hook_close ? down : up;
            SetLabelColorStyle(hTmp, nStyle);
            InvalidateRect(hTmp, NULL, TRUE);
        }

        if(feedback.board_zero != last_feedback.board_zero)
        {
            hTmp = GetDlgItem(hWnd, IDC_LABEL1 + 2);
            nStyle = feedback.board_zero ? down : up;
            SetLabelColorStyle(hTmp, nStyle);
            InvalidateRect(hTmp, NULL, TRUE);
        }

        if(feedback.board_high != last_feedback.board_high)
        {
            hTmp = GetDlgItem(hWnd, IDC_LABEL1 + 3);
            nStyle = feedback.board_high ? down : up;
            SetLabelColorStyle(hTmp, nStyle);
            InvalidateRect(hTmp, NULL, TRUE);
        }

        if(feedback.board_safe != last_feedback.board_safe)
        {
            hTmp = GetDlgItem(hWnd, IDC_LABEL1 + 4);
            nStyle = feedback.board_safe ? down : up;
            SetLabelColorStyle(hTmp, nStyle);
            InvalidateRect(hTmp, NULL, TRUE);
        }

        if(feedback.roll_open != last_feedback.roll_open)
        {
            hTmp = GetDlgItem(hWnd, IDC_LABEL1 + 5);
            nStyle = feedback.roll_open ? down : up;
            data.board_test.roll_open = feedback.roll_open;
            SetLabelColorStyle(hTmp, nStyle);
            InvalidateRect(hTmp, NULL, TRUE);
        }

        /*hTmp = GetDlgItem(hWnd, IDC_LABEL1 + 6);
        nStyle = feedback.ray ? down : up;
        SetLabelColorStyle(hTmp, nStyle);
        InvalidateRect(hTmp, NULL, TRUE);*/

        if(feedback.clip1 != last_feedback.clip1)
        {
            hTmp = GetDlgItem(hWnd, IDC_LABEL1 + 7);
            nStyle = feedback.clip1 ? down : up;
            SetLabelColorStyle(hTmp, nStyle);
            InvalidateRect(hTmp, NULL, TRUE);
        }

        if(feedback.clip2 != last_feedback.clip2)
        {
            hTmp = GetDlgItem(hWnd, IDC_LABEL1 + 8);
            nStyle = feedback.clip2 ? down : up;
            SetLabelColorStyle(hTmp, nStyle);
            InvalidateRect(hTmp, NULL, TRUE);
        }

        if(feedback.clip3 != last_feedback.clip3)
        {
            hTmp = GetDlgItem(hWnd, IDC_LABEL1 + 9);
            nStyle = feedback.clip3 ? down : up;
            SetLabelColorStyle(hTmp, nStyle);
            InvalidateRect(hTmp, NULL, TRUE);
        }

        if(feedback.clip4 != last_feedback.clip4)
        {
            hTmp = GetDlgItem(hWnd, IDC_LABEL1 + 10);
            nStyle = feedback.clip4 ? down : up;
            SetLabelColorStyle(hTmp, nStyle);
            InvalidateRect(hTmp, NULL, TRUE);
        }

        if(feedback.scissor1 != last_feedback.scissor1)
        {
            hTmp = GetDlgItem(hWnd, IDC_LABEL1 + 11);
            nStyle = feedback.scissor1 ? down : up;
            SetLabelColorStyle(hTmp, nStyle);
            InvalidateRect(hTmp, NULL, TRUE);
        }

        if(feedback.scissor2 != last_feedback.scissor2)
        {
            hTmp = GetDlgItem(hWnd, IDC_LABEL1 + 12);
            nStyle = feedback.scissor2 ? down : up;
            SetLabelColorStyle(hTmp, nStyle);
            InvalidateRect(hTmp, NULL, TRUE);
        }

        hTmp = GetDlgItem(hWnd, IDC_LABEL1 + 13);
        const char *psc = GetWindowCaption(hTmp);
        if(atoi(psc) != feedback.coder)
        {
            char sc[64] = { 0 };
            sprintf(sc, "%d", feedback.coder);
            SetWindowCaption(hTmp, sc);
            InvalidateRect(hTmp, NULL, TRUE);
        }

        //high roller
        if(data.board_test.roll_act)
        {
            //printf("roll_wait: %d\n", data.board_test.roll_wait);
            if(feedback.roll_open)
            {
             //   RunRab(g_knit_data, 0);    //**
                data.board_test.roll_act = FALSE;
            }
            else if(data.board_test.roll_wait > 30)
            {
                data.board_test.roll_act = FALSE;
            }
            else
            {
                ++data.board_test.roll_wait;
            }
        }
        //board
        if(data.board_test.board_act)
        {
            //printf("board_wait: %d\n", data.board_test.board_wait);
            if(!feedback.board_sj)
            {
                data.board_test.board_act = FALSE;
            }
            else if(data.board_test.board_wait > 60)
            {
                data.board_test.board_act = FALSE;
            }
            else
            {
                ++data.board_test.board_wait;
            }
        }
        if(data.board_test.board_reset)
        {
            if(feedback.hook_open)
            {
                data.board_test.board_reset = FALSE;
                BoardAct(g_knit_data, BOARD_RESET);
                data.board_test.board_act = TRUE;
                data.board_test.board_wait = 0;
            }
        }
        //clip and scissor
        if(data.board_test.cs_act)
        {
            if(feedback.cs_error == 0x0F)
            {
                if(data.board_test.cs_auto)
                {
                    ++data.board_test.cs_auto;
                    switch(data.board_test.cs_auto)
                    {
                    case 1:
                        ClipOff(g_knit_data, 0x01);
                        break;
                    case 2:
                        ClipOff(g_knit_data, 0x02);
                        break;
                    case 3:
                        Scissor(g_knit_data, 0x01);
                        break;
                    case 4:
                        ClipOff(g_knit_data, 0x04);
                        break;
                    case 5:
                        ClipOff(g_knit_data, 0x08);
                        break;
                    case 6:
                        Scissor(g_knit_data, 0x02);
                        break;
                    case 7:
                        {
                            data.board_test.cs_act = FALSE;
                            data.board_test.cs_auto = 0;
                        }
                        break;
                    }
                }
                else if(data.board_test.cs_syn)
                {
                    Scissor(g_knit_data, data.board_test.cs_syn);
                    data.board_test.cs_syn = 0;
                    data.board_test.cs_wait = 0;
                }
                else
                {
                    data.board_test.cs_act = FALSE;
                }
            }
            else if(data.board_test.cs_wait > 30)
            {
                printf("cs_error: %d\n", feedback.cs_error);
                data.board_test.cs_act = FALSE;
                data.board_test.cs_syn = 0;
                data.board_test.cs_auto = 0;
            }
            else
            {
                ++data.board_test.cs_wait;
            }
        }
        memcpy(&last_feedback, &feedback, sizeof(feedback));
    }
    SetWindowAdditionalData(hWnd, data.value);
}
/*
 *
 */
static int BoardTestWinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
    case MSG_CREATE:
        {
            CreateBoardTestInterface(hWnd);
        }
        break;
    case MSG_PAINT:
        {
            HDC hdc = GetClientDC(hWnd);
            RECT rect;
            GetClientRect(hWnd, &rect);
            rect.left = 10;
            rect.top += MASTER_TITLE_TOP;
            rect.right = 100;
            rect.bottom = rect.top + MASTER_TITLE_HEIGHT - 1;
            SetBkMode(hdc, BM_TRANSPARENT);
            SetTextColor(hdc, COLOR_HQ_GREEN(hdc));
            DrawText(hdc, GetWindowCaption (hWnd), -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
            ReleaseDC(hdc);
        }
        break;
    case MSG_CLOSE:
        {
            HWND hHosting = GetHosting(hWnd);
            if(hHosting != HWND_INVALID)
            {
                EnableWindow(hHosting, TRUE);
            }
            DestroyMainWindow(hWnd);
            MainWindowCleanup(hWnd);
        }
        return 0;
    case MSG_DESTROY:
        {
            ShowWindow(hWnd, SW_HIDE);
            DestroyAllControls(hWnd);
        }
        break;
    case MSG_KEYDOWN:
        {
            OnKeyDown(hWnd, wParam);
        }
        return 0;
    case MSG_ERASEBKGND:
        {
            DrawBitmapBkGnd(hWnd, (RECT*)lParam, &g_bmp[BMP_BOARD_TEST]);
        }
        return 0;
    case MSG_COMMAND:
        {
            OnBtnCmd(hWnd, LOWORD(wParam));
        }
        return 0;
    case MSG_IDLE:
        {
            OnIdle(hWnd);
        }
        break;
    }
    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}
/*
 *
 */
HWND CreateBoardTestWindow(HWND hHosting)
{
    int x = (CX_SCREEN - CX_BOARD_TEST_BMP) / 2;
    int y = (CY_SCREEN - CY_BOARD_TEST_BMP) / 2;
    MAINWINCREATE CreateInfo;
    CreateInfo.dwStyle = WS_VISIBLE;
    CreateInfo.dwExStyle = WS_EX_NONE;
    CreateInfo.spCaption = GetString(31);
    CreateInfo.hMenu = 0;
    CreateInfo.hCursor = GetSystemCursor(0);
    CreateInfo.hIcon = 0;
    CreateInfo.MainWindowProc = BoardTestWinProc;
    CreateInfo.lx = x;
    CreateInfo.ty = y;
    CreateInfo.rx = x + CX_BOARD_TEST_BMP;
    CreateInfo.by = y + CY_BOARD_TEST_BMP;
    CreateInfo.iBkColor = COLOR_lightwhite;
    CreateInfo.dwAddData = 0;
    CreateInfo.hHosting = hHosting;
    HWND hWnd = CreateMainWindow(&CreateInfo);
    if(hWnd != HWND_INVALID)
    {
        SwitchToKnitState(g_knit_data, FALSE);
        EnableWindow(hHosting, FALSE);
        SetFocusChild(GetDlgItem(hWnd, ID_BUTTON1));
        ShowWindow(hWnd, SW_SHOWNORMAL);
    }
    return hWnd;
}
